﻿using System;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

using ipi.MCC.Controls;
using ipi.MCC.Global;

namespace ipi.MCC.WebParts
{
    public interface IMCCSelectNode
    {
        int GetNodeID { get;}
    }
    
    [Guid("bee4575c-9cf5-40bb-ace9-b033512734b2")]
    public class MCCTreeView : MCCAjaxBaseWebPart, IMCCSelectNode
    {
        protected MCCTreeControl ucTree;
        private int SelectedNode = -1;

        private string _RootNode;
        [WebDescription(""), WebDisplayName("Name of root node"), Personalizable(PersonalizationScope.User), WebBrowsable(true)]
        public string RootNode
        {
            get { return _RootNode; }
            set { _RootNode = value; }
        }

        private string sStartNodeID = "0";
        [WebDescription(""), WebDisplayName("NodeID the tree starts with"), Personalizable(PersonalizationScope.Shared), WebBrowsable(false)]
        public string StartNodeID
        {
            set
            {
                sStartNodeID = value;
            }

            get
            {
                return sStartNodeID;
            }
        }
        
        protected override void Render(HtmlTextWriter writer)
        {
            // TODO: add custom rendering code here.
            // writer.Write("Output HTML");
            
            EnsureChildControls();

            // The appropriate child control will render based on what was added to the collection
            try
            {
                RenderChildren(writer);
            }
            catch (System.Exception exc)
            {
                writer.WriteLine("The following error occurred in this Web Part.  Please contact your administrator for further assistance. \n" + Exceptions.HandleException(exc, this.GetType(), MethodBase.GetCurrentMethod(), LoggingType.Global_Exception));
            }
        }

        protected override void CreateChildControls()
        {
            try
            {
                this.Controls.Clear();
                
                ucTree = (MCCTreeControl)this.Page.LoadControl(@"/_controltemplates/MCC/MCCTreeControl.ascx");
                ucTree.RootNode = _RootNode;
                ucTree.StartNodeID = sStartNodeID;
                ucTree.OnNodeSelected += new OnNodeSelectedHandler(ucTree_OnNodeSelected);
                this.Controls.Add(ucTree);
            }
            catch (Exception exc)
            {
                System.Diagnostics.Debug.WriteLine(Exceptions.HandleException(exc, this.GetType(), MethodBase.GetCurrentMethod(), LoggingType.Global_Exception));
            }
        }

        public void ForceRefresh()
        {
            ucTree.StartNodeID = sStartNodeID;
            ucTree.PreSelected = sStartNodeID;
            ucTree.CreateNewTree();
//            SelectedNode = Convert.ToInt32(sStartNodeID);
        }
        
        public override ToolPart[] GetToolParts()
        {

            ToolPart[] toolparts = new ToolPart[3];
            WebPartToolPart wptp = new WebPartToolPart();
            CustomPropertyToolPart custom = new CustomPropertyToolPart();
            MCCTreeViewToolPart tvpMain = new MCCTreeViewToolPart();
            toolparts[0] = tvpMain;
            toolparts[1] = wptp;
            toolparts[2] = custom;

            return toolparts;
        }
        
        void ucTree_OnNodeSelected(System.Web.UI.WebControls.TreeNode CurrentNode)
        {
            SelectedNode = Convert.ToInt32(CurrentNode.Value);
        }

        [ConnectionProvider("Node Provider")]
        public IMCCSelectNode ConnectionInterface()
        {
            return this;
        }

        public int GetNodeID
        {
            get { return SelectedNode; }
        }
    }
}
